<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Object user = request.getSession().getAttribute("user");
if(user==null){
	response.sendRedirect(basePath+"login");
}
%>


<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>${sysconf.proname}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="css/xadmin.css">
    <script type="text/javascript" src="js/jquery1.min.js"></script>
    <script type="text/javascript" src="js/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/xadmin.js"></script>
    <script type="text/javascript" src="js/datautil.js"></script>
	<style type="text/css">
		.layui-input-block{margin: 0px}
		.hide{display: none}
	</style>
  </head>
  
 <body>
 	<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>账号管理</a><a><cite>账号列表</cite></a>
      </span>
      <a class="layui-btn layui-btn-small sxBtn" href="javascript:location.replace(location.href);" title="刷新">
      <i class="layui-icon">&#xe669;</i></a>
    </div>
     <div class="x-body">
		<blockquote class="layui-elem-quote">账号列表</blockquote>		
			<form class="layui-form" action="">
				<div class="layui-form topSear">
				  <div class="layui-form-item">
				  	<div class="layui-inline">
				      <!-- <label class="layui-form-label">报表名称：</label>
				      <div class="layui-input-inline">
				        <input type="text" name="name" class="layui-input" placeholder="输入任务名称"/>
				      </div> -->
				      <div class="layui-input-inline">
					    <!-- <a class="layui-btn layui-btn-normal" onclick="layer.msg('该功能暂未开放')">查询</a> -->
					    <a class="layui-btn layui-btn-normal" onclick="edit({})">添加</a>
					  </div>
				    </div>
				   		   
				  </div>
				</div>
			</form>
			<div class="topBox">
				
			</div>
			 <table class="layui-table" id="tabList"  lay-data="{height:'550',url:'account/findList',parseData:function(data){return parseData(data)},page:true, id:'tabList'}"  lay-filter="tabList">
			<!-- <table class="layui-table"  id="tabList" lay-data="{height:'550', url: 'json/list-1.json',parseData:function(data){return parseData(data)},page:true, id:'tabList'}" lay-filter="tabList"> -->
				<thead>
			    <tr>
			      <th lay-data="{field:'ID', width:80}">序号</th>
			      <th lay-data="{field:'ACCOUNT', width:150}">登录账号</th>
			      <th lay-data="{field:'USERNAME',minWidth:100}">用户名</th>
			      <th lay-data="{field:'PHONE', width:120}">手机号码</th>
			      <th lay-data="{field:'OPENID', width:270}">OPENID</th>
			      <th lay-data="{field:'', width:100,templet:function(res){return res.ROLE==1?'管理员':'普通账号'}}">账号类型</th>
			      <th lay-data="{field:'DEPARTMENT', width:100}">部门</th>
			      <th lay-data="{field:'', width:80,templet:function(res){return res.STATUS==1?'启用':'停用'}}">状态</th>
			      <th lay-data="{field:'CREATETIME', width:160}">添加时间</th>
			     <th lay-data="{fixed: 'right', width:150,templet: '#caozuo'}">操作</th>
			    </tr>
			  </thead>
			</table>
			<script type="text/html" id="caozuo">
  				<a class="layui-btn layui-btn-primary layui-btn-xs {{d.ROLE==${user.role}?'layui-btn-disabled':''}}" lay-event="{{d.ROLE==${user.role}?'':'edit'}}"><i class="layui-icon">&#xe642;</i>编辑<a>
  				<a class="layui-btn layui-btn-primary layui-btn-xs {{d.ROLE==${user.role}?'layui-btn-disabled':''}}" lay-event="{{d.ROLE==${user.role}?'':'upstatus'}}"><i class="layui-icon"> &#x{{ d.STATUS == 1 ? "1006;" : "e605;" }}</i>{{ d.STATUS == 1 ? '停用' : '启用' }}</a>
			</script>
		</div>

		<div class="edit" style="display: none">
			<form class="layui-form" style="margin: 1px" lay-filter="statementEdit_temp">
				<table class="layui-table insertTable">
				  <tbody>
				  	<tr>
				  	  <input type="hidden" name="id" value="" >
				      <td align="right" width="105"><span style="color: #f00;">*</span>账号类型：</td>
				      <td>
				      	<div class="layui-input-inline">
				      	  <select name="role" value="" >
				        	<option value="2">普通账号</option>
							<c:if test="${user.role eq 0}">
					        	<option value="1">管理员</option>
							</c:if>
					      </select>
				      	</div>
				      </td>

				    </tr>
				  	<tr>
				  	  <input type="hidden" name="id" value="" >
				      <td align="right" width="105"><span style="color: #f00;">*</span>登录账号：</td>
				      <td>
				      	<div class="layui-input-inline">
				      		<input type="text" name="account" value="" class="layui-input" required="required">
				      	</div>
				      </td>

				    </tr>
				    
				    <tr>
				      <td align="right" width="105"><span style="color: #f00;">*</span>用户名：</td>
				      <td>
				      	<div class="layui-input-inline">
				      		<input type="text" name="username" value="" class="layui-input" required="required">
				      	</div>
				      </td>
				      
				      
				    </tr>	
				    <tr>
				      <td align="right" width="105"><span style="color: #f00;"></span>手机号码：</td>
				      <td>
				      	<div class="layui-input-inline">
				      		<input type="text" name="phone" value="" class="layui-input" >
				      	</div>
				      </td>
				      
				    </tr>
				    
				    <tr>
				      <td align="right" width="105"><span style="color: #f00;"></span>OPENID：</td>
				      <td>
				      	<div class="layui-input-inline">
				      		<input type="text" name="openid" value="" class="layui-input" >
				      	</div>
				      </td>
				      
				    </tr>
				    <tr>
				      <td align="right" width="105"><span style="color: #f00;">*</span>部门：</td>
				      <td>
				      	<div class="layui-input-inline">
				      		<input type="text" name="department" value="" class="layui-input" required="required">
				      	</div>
				      </td>

				    </tr>		
				   
				   
				  </tbody>
				</table>
			</form>
		</div>
	
<script type="text/javascript" src="js/layui_exts/exts.js"></script>
<script type="text/javascript" src="js/datautil.js"></script>
<script>

//返回的数据解析格式
function parseData(data){
	return {
      "code": data.code, //解析接口状态
      "msg": data.msg, //解析提示文本
      "count": data.count, //解析数据长度
      "data": data.list //解析数据列表
    };
}

var acc_limits = {};

layui.use(['table','laydate','tableSelect','form', 'layer','element'], function(){
  table = layui.table; 
  form = layui.form;
  tableSelect = layui.tableSelect;
  var laydate = layui.laydate;
  var laypage = layui.laypage,layer = layui.layer;	
  
   form.on('checkbox(limits)', function(data){
	  console.log(data.elem); //得到checkbox原始DOM对象
	  console.log(data.elem.checked); //是否被选中，true或者false
	  console.log(data.value); //复选框value值，也可以通过data.elem.value得到
	  console.log(data.othis); //得到美化后的DOM对象
	  var ischecked = data.elem.checked
	  var $input = $(data.elem);
	  var value = data.value;
	  var id = $input.attr("data-id");

	  var limitstr = acc_limits[id];
	  if(ischecked){
		  if(limitstr==null){
			  acc_limits[id] = 'SE|'+ value;
		  }else{
			  acc_limits[id] = limitstr+'|'+ value;
		  }
	  }else{
		  if(limitstr!=null){
			  acc_limits[id] = limitstr.replace('\|'+value,'');
		  }
	  }
	  
	  console.log(JSON.stringify(acc_limits));
	});  
  
  table.on('tool(tabList)',function(res){
	var data = res.data;
	switch (res.event) {
	case 'auth':
		//layer.msg("给账号授权")
		auth(data);
		break;
	case 'edit':
		edit(data);
		break;
	case 'upstatus':
		upstatus(data);
		break;

	default:
		break;
	}
  })
  

  
})


function auth(data){
	layer.open({
		type: 2,
      	title: "新增",
     	shadeClose: true,		      
      	maxmin: true, //开启最大化最小化按钮
      	area: ['1000px', '600px'],
      	content: 'authList?id='+data.ID
	})
	
}




/**编辑*/
function edit(data){
	var html = $(".edit").html().replace('statementEdit_temp','statementEdit');
	layer.open({
      type: 1,
      title: "新增",
      shadeClose: true,		      
      maxmin: true, //开启最大化最小化按钮
      area: ['500px', '420px'],
      content: html,
      btn: ['保存', '取消'],
	  success: function(layero, index){
		  	data = toLowerCase(data);
			form.val("statementEdit",data);
			form.render();
	  },
	  yes: function(index, layero){
		  
		    var formdata = layero.find("form").serializeArray();
		    
		    var d = {};
			$.each(formdata, function () {
		    	 var v = this.value;
		    	 if(v!="")d[this.name] = v; 
		     });
		    
		    console.log("data:"+JSON.stringify(d))
		    
		    $.post("account/save",d,function(data){
		    	if(data.res==1){
		    		layer.close(index);
		    		layer.msg("保存成功")
		    		$(".layui-laypage-btn").click();;
		    		
		    	}else{
		    		layer.msg(data.msg);
		    	}
		    	
		    },"JSON")
		    
		    /* layer.close(index); */ //如果设定了yes回调，需进行手工关闭
	  }
    });
	
}

//状态修改
function upstatus(data){
	var status = data.STATUS
	$.post("account/upstatus",{"id":data.ID,"status":status==0?1:0},function(data){
		if(data.res==1){
			layer.msg("操作成功");
			$(".layui-laypage-btn").click();;
		}else{
			layer.msg(data.msg)
		}
		
	})
	
}



</script>

</body>

</html>